/**
 * Title :	jFamilyTree generator
 * Description: jFamilyTree generator
 * Version:	provider
 * Copyright (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package jFamilyTree.site;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

import org.w3c.dom.Document;

public class FileSiteMaker extends SiteMaker
{
	/**
	 * Path url
	 */
	private String thePathUrl;

	/**
	 * Extra path
	 */
	private String theXtraPath = ""; //$NON-NLS-1$

	/**
	 * Constructor
	 * 
	 * @param thisDocument
	 *        document
	 * @param thisRepositoryFolder
	 *        repository folder (source)
	 * @param thisImageFolder
	 *        image folder (source)
	 * @param thisPath
	 *        path (source)
	 * @param thisHtmlFile
	 *        HTML file (target)
	 * @param thisXmlFile
	 *        XML file (target)
	 * @param thisTitle
	 *        HTML file title (target)
	 * @throws URISyntaxException
	 * @throws IOException
	 */
	public FileSiteMaker(Document thisDocument, String thisRepositoryFolder, String thisImageFolder, String thisPath, String thisHtmlFile, String thisXmlFile, String thisTitle) throws URISyntaxException, IOException
	{
		super(thisDocument, thisRepositoryFolder, thisImageFolder, thisHtmlFile, thisXmlFile, thisTitle);

		File thisFolder = new File(thisPath);
		if (!thisFolder.exists() || !thisFolder.isDirectory())
			throw new FileNotFoundException(thisPath + " not found"); //$NON-NLS-1$

		thePathUrl = thisFolder.toURI().toURL().toString();
	}

	/*
	 * (non-Javadoc)
	 * @see jFamilyTree.site.SiteMaker#connect()
	 */
	public boolean connect()
	{
		return true;
	}

	/*
	 * (non-Javadoc)
	 * @see jFamilyTree.site.SiteMaker#disconnect()
	 */
	public boolean disconnect()
	{
		return true;
	}

	/*
	 * (non-Javadoc)
	 * @see jFamilyTree.site.SiteMaker#makeOutStream(java.lang.String)
	 */
	public OutputStream makeOutStream(String thisFilename) throws MalformedURLException, IOException, URISyntaxException
	{
		URL thisPathUrl = new URL(thePathUrl);
		URL thisUrl = new URL(thisPathUrl, theXtraPath + thisFilename);
		File thisFile = new File(thisUrl.toURI());
		return new FileOutputStream(thisFile);
	}

	/*
	 * (non-Javadoc)
	 * @see jFamilyTree.site.SiteMaker#changeFolder(java.lang.String)
	 */
	public boolean changeFolder(String thisRelativeFolder) throws MalformedURLException, URISyntaxException
	{
		String thisXtraPath = thisRelativeFolder;
		if (thisXtraPath == null || thisXtraPath.isEmpty())
			return true;
		URL thisPathUrl = new URL(thePathUrl);
		URL thisUrl = new URL(thisPathUrl, thisXtraPath);
		File thisFolder = new File(thisUrl.toURI());
		if (!thisFolder.exists())
		{
			if (!thisFolder.mkdirs())
				return false;
		}
		// update extra path
		theXtraPath = thisXtraPath;
		if (theXtraPath != "" && !theXtraPath.endsWith("/")) //$NON-NLS-1$ //$NON-NLS-2$
			theXtraPath += "/"; //$NON-NLS-1$
		return true;
	}
}
